-- Colosseum!

function load()

 love.graphics.setMode(640, 400, false, true, 0)
 love.graphics.setBackgroundColor(25, 25, 25)
 music = love.audio.newMusic("bg_loop.ogg")
 love.audio.play(music, 0)

 local font = love.graphics.newFont("Press Start K.ttf", 8) 
      love.graphics.setFont(font)


	-- world
   world = love.physics.newWorld(640, 400) 
	walls = love.physics.newBody(world, 0, 0, 0) 
	top_wall = love.physics.newRectangleShape(walls, 1, 1, 2000, 1)
	left_wall = love.physics.newRectangleShape(walls, 1, 1, 1, 2000)
   right_wall = love.physics.newRectangleShape(walls, 640, 1, 1, 2000) 
	bottom_wall = love.physics.newRectangleShape(walls, 1, 400, 2000, 1) 

	-- player
   math.randomseed(os.time())
   px = math.random(640)
   py = math.random(400)
   player = love.graphics.newImage("/images/soldierStandingSouth.png")
   playerBody = love.physics.newBody(world, px, py)
   setPlayerAngle = 0
   playerCircle = love.physics.newCircleShape(playerBody, 10)
   playerDirection = "standing"
   playerBody:setMass(0,0,100 * math.pi,5000 * math.pi)
   playerBody:setDamping(7)
   playerBody:setAngularDamping(7)
   playerSpeed = 100
   playerFrames = 0
   animationSpeed = 0

	-- zombie
   math.randomseed(os.time()*2)
   zx = math.random(640)
   zy = math.random(400)
   zombie = love.graphics.newImage("zombie.png")
   zombieBody = love.physics.newBody(world, zx, zy)
   zombieCircle = love.physics.newCircleShape(zombieBody, 5)
   zombieBody:setMassFromShapes()
   zombieBody:setDamping(1)

	-- mouse
   love.mouse.setVisible(false)
   crossHair = love.graphics.newImage("/images/cross.png")

end

--

function update(dt)

	world:update(dt)
	px, py = playerBody:getPosition()
   zx, zy = zombieBody:getPosition()

   -- There has to be a simpler way to do this:
   if px - zx > 0 then 
		zombieRunX = 1
	elseif px - zx < 0 then 
		zombieRunX = -1
		else zombieRunX = 0
   end

   if py - zy > 0 then 
		zombieRunY = 1
   elseif py - zy < 0 then 
		zombieRunY = -1
   else zombieRunY = 0
   end

	zombieBody:applyImpulse(zombieRunX * 100, zombieRunY * 100)

   math.randomseed(os.time())

	-- west and east player movement
   if not love.keyboard.isDown(love.key_s)
	and not love.keyboard.isDown(love.key_w) then
      if love.keyboard.isDown(love.key_d) then
         setPlayerAngle = 90
         playerBody:applyImpulse(40 * playerSpeed, 0)
         playerAction = "walking"
         playerFacing = "east"
      elseif love.keyboard.isDown(love.key_a) then
         setPlayerAngle = 270
         playerBody:applyImpulse(-40 * playerSpeed, 0)
         playerAction = "walking"
         playerFacing = "west"
      end
   end

	-- north and south player movement
   if not love.keyboard.isDown(love.key_a)
   and not love.keyboard.isDown(love.key_d) then
      if love.keyboard.isDown(love.key_s) then
        setPlayerAngle = 180
         playerBody:applyImpulse(0, 40 * playerSpeed)
         playerAction = "walking"
         playerFacing = "south"
      elseif love.keyboard.isDown(love.key_w) then
         setPlayerAngle = 0
         playerBody:applyImpulse(0, -40 * playerSpeed)
         playerAction = "walking"
         playerFacing = "north"
      end
   end

	-- northeast and southwest player movement
   if love.keyboard.isDown(love.key_w) and
      love.keyboard.isDown(love.key_d) then
      setPlayerAngle = 45
      playerBody:applyImpulse(100/3 * playerSpeed, -100/3 * playerSpeed)
      playerAction = "walking"
      playerFacing = "northeast"
   elseif love.keyboard.isDown(love.key_s) and
      love.keyboard.isDown(love.key_a) then
      setPlayerAngle = 225
      playerBody:applyImpulse(-100/3 * playerSpeed, 100/3 * playerSpeed)
      playerAction = "walking"
      playerFacing = "southwest"

	 -- northwest and southeast player movement
   elseif love.keyboard.isDown(love.key_w) and
      love.keyboard.isDown(love.key_a) then
      setPlayerAngle = 315
      playerBody:applyImpulse(-100/3 * playerSpeed, -100/3 * playerSpeed)
      playerAction = "walking"
      playerFacing = "northwest"
   elseif love.keyboard.isDown(love.key_s) and
      love.keyboard.isDown(love.key_d) then
      playerBody:applyImpulse(100/3 * playerSpeed, 100/3 * playerSpeed)
      setPlayerAngle = 135
      playerAction = "walking"
      playerFacing = "southeast"
   end

   if not love.keyboard.isDown(love.key_w) and
   not love.keyboard.isDown(love.key_a) and
   not love.keyboard.isDown(love.key_s) and
   not love.keyboard.isDown(love.key_d) then
      playerAction = "standing"
   end
   playerBody:setAngle(0)

	-- This is way too complex for what it does. D: AND NOW IT'S USELESS
   --[[rotatePlayer = "neither"
   if math.ceil(playerBody:getAngle()) ~= setPlayerAngle + 180
   or math.ceil(playerBody:getAngle()) ~= setPlayerAngle - 180 then
      if playerBody:getAngle() >= 270 and setPlayerAngle <= 90 then
         rotatePlayer = "clockwise"
      elseif playerBody:getAngle() <= 90 and setPlayerAngle >= 270 then
         rotatePlayer = "counterclockwise"
      else
         if playerBody:getAngle() < setPlayerAngle then
            rotatePlayer = "clockwise"
         elseif playerBody:getAngle() > setPlayerAngle then
            rotatePlayer = "counterclockwise"
         end
      end
   else
      math.randomseed(os.time()+1)
      if math.random() == 0 then
         rotatePlayer = "clockwise"
      else
         rotatePlayer = "counterclockwise"
      end
   end
   if math.ceil(playerBody:getAngle()) ~= setPlayerAngle then
      if rotatePlayer == "clockwise" then
         playerBody:setAngle(playerBody:getAngle() + 5)
      elseif rotatePlayer == "counterclockwise" then
         playerBody:setAngle(playerBody:getAngle() - 5)
      end
   else rotatePlayer = "neither"
   end

   if playerBody:getAngle() >= 360 then
      playerBody:setAngle(playerBody:getAngle() - 360)
   elseif playerBody:getAngle() < 0 then
      playerBody:setAngle(playerBody:getAngle() + 360)
   end
   ]]--

   animationSpeed = animationSpeed + 100 * dt
end

--

function draw()

   if playerAction == "walking" then
      if playerFacing == "south" then
         if animationSpeed >= 12 then
            if playerFrames == 0 then
               playerFrames = 2
            elseif playerFrames == 2 then
               player = love.graphics.newImage("/images/soldierWalkSouth1.png")
               playerFrames = playerFrames - 1
               animationSpeed = 0
            elseif playerFrames == 1 then
               player = love.graphics.newImage("/images/soldierWalkSouth2.png")
               playerFrames = playerFrames - 1
               animationSpeed = 0
            end
         end
      end
      
   elseif playerAction == "standing" then
      if playerFacing == "south" then
         player = love.graphics.newImage("/images/soldierStandingSouth.png")
      elseif playerFacing == "north" then
         player = love.graphics.newImage("/images/soldierStandingNorth.png")
      elseif playerFacing == "east" then
         player = love.graphics.newImage("/images/soldierStandingEast.png")
      elseif playerFacing == "west" then
         player = love.graphics.newImage("/images/soldierStandingWest.png")
      end
   end
   love.graphics.draw(player, playerBody:getX(), playerBody:getY(), playerBody:getAngle())

   love.graphics.draw(zombie, zombieBody:getX(), zombieBody:getY(), zombieBody:getAngle())

	-- crosshair
	local x, y = love.mouse.getPosition()
	love.graphics.draw((crossHair), x, y)

	-- player coords
   love.graphics.setColor(255, 191, 0)
	love.graphics.draw("Player: " .. math.ceil(px) .. " X, " .. math.ceil(py) .. " Y\nMouse:  " .. x .. " X, " .. y .. " Y", 8, 20)

  -- draws the ground
   love.graphics.setColor(255, 255, 255)
   love.graphics.polygon(love.draw_line, top_wall:getPoints())
	love.graphics.polygon(love.draw_line, right_wall:getPoints())
	love.graphics.polygon(love.draw_line, bottom_wall:getPoints())
	love.graphics.polygon(love.draw_line, left_wall:getPoints())

end